package com.h3c.ptability.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.h3c.ptability.dto.form.HistoryInfo;
import com.h3c.ptability.entity.PtAllWorker;
import com.h3c.ptability.mapper.ApprovalRecordMapper;
import com.h3c.ptability.entity.ApprovalRecord;
import com.h3c.ptability.mapper.PtAllWorkerMapper;
import com.h3c.ptability.service.IApprovalRecordService;
import com.h3c.ptability.utils.CommonUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* 实现类
*
* @author makejava
* @since 2022-08-24 13:49:37
*/
@Service
public class ApprovalRecordServiceImpl extends ServiceImpl<ApprovalRecordMapper, ApprovalRecord> implements IApprovalRecordService {

    @Resource
    private ApprovalRecordMapper approvalRecordMapper;
    @Resource
    private PtAllWorkerMapper ptAllWorkerMapper;

    /**
     * 构建表单 historyInfos 部分数据
     */
    @Override
    public List<HistoryInfo> getHistoryInfos(String flowId) {
        //流转记录
        QueryWrapper<ApprovalRecord> approvalQW = new QueryWrapper<>();
        approvalQW.lambda().eq(ApprovalRecord::getFlowId, flowId)
                .orderByDesc(ApprovalRecord::getApprovalTime);
        List<ApprovalRecord> approvalRecords = approvalRecordMapper.selectList(approvalQW);
        List<String> userIds = approvalRecords.stream()
                .map(ApprovalRecord::getApprovalUserId).collect(Collectors.toList());
        if (userIds.isEmpty()) {
            return new ArrayList<>();
        }

        //用户信息 approvalUserId
        QueryWrapper<PtAllWorker> allWorkerQW = new QueryWrapper<>();
        allWorkerQW.lambda().in(PtAllWorker::getWorkerId, userIds);
        List<PtAllWorker> allWorkers = ptAllWorkerMapper.selectList(allWorkerQW);
        Map<String, PtAllWorker> domainAccountMap = allWorkers.stream()
                .collect(Collectors.toMap(PtAllWorker::getWorkerId, o -> o));

        List<HistoryInfo> mapList = new ArrayList<>();
        approvalRecords.forEach(record -> {

            //流转人信息
            PtAllWorker allWorker = domainAccountMap.get(record.getApprovalUserId());
            JSONObject approver = new JSONObject();
            if (allWorker != null) {
                approver.put("empNumber", allWorker.getDomainAccount());
                approver.put("name", allWorker.getWorkerName());
            }

            //流转信息
            HistoryInfo historyInfo = new HistoryInfo();
            historyInfo.setApprover(approver);
     /*       TodoTaskNodeEnum enumByValue = TodoTaskNodeEnum.getEnumByValue(record.getApprovalType());
            historyInfo.setCurrentNode(enumByValue.getName());*/
            historyInfo.setApproveTime(record.getApprovalTime());
            historyInfo.setApproveOpinion(CommonUtil.null2Empty(record.getApprovalNote()));
            mapList.add(historyInfo);
        });
        return mapList;
    }


}

